﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class ThoGiaCongDAO
    {
        private static QUANLYTIEMVANGEntities dbContext = new QUANLYTIEMVANGEntities();

        public static THOGIACONG SelectThoGiaCongByMaThoGiaCong(uint matho)
        {
            var query = from d in dbContext.THOGIACONGs
                        where d.MaTho == matho
                        select d;
            if (query != null)
                return query.First();
            return null;
        }

        public static List<THOGIACONG> GetAllThoGiaCong()
        {
            var query = from d in dbContext.THOGIACONGs
                        select d;
            if (query == null)
                return null;
            return query.ToList();           
        }

        public static void Insert(THOGIACONG tgc)
        {
            dbContext.THOGIACONGs.Add(tgc);
            dbContext.SaveChanges();            
        }

        // watch out this function
        public static void Delete(THOGIACONG tgc)
        {
            dbContext.THOGIACONGs.Remove(tgc);
            dbContext.SaveChanges();
        }

        public static void Update(THOGIACONG tgc)
        {
            var query=dbContext.THOGIACONGs.Find(tgc.MaTho);
            if (query != null)
            {
                query.DiaChi = tgc.DiaChi;
                query.TenTho = tgc.TenTho;
                dbContext.SaveChanges();

            }
            else
                throw new Exception("Không tìm thấy thợ trong CSDL!");
        }

        // news Quy
        public static List<int> GetAllThoGiaCongId()
        {
            var query = from d in dbContext.THOGIACONGs
                        select d.MaTho;
            if (query == null)
                return null;
            List<int> list = query.ToList();
            return query.ToList();
        }

        public static int GetLastMaThoGiaCong()
        {
            var query = (from d in dbContext.THOGIACONGs
                         select d.MaTho).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
